var app=new Vue({
    el: '#app',
    data:{
        deptTree: null,
        deptName: ''
    },
    watch: {
        deptName: function(val) {
            this.$refs.deptTree.filter(val);
        }
    },
    methods:{
        init: function(){
            var _this=this;
            setTimeout(function(){
                _this.getDeptTree();
            },100);
        },
        /**
         * 过滤部门
         */
        filterName:function(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        addDept: function(){
            var node=this.$refs.deptTree.getCurrentNode();
            top.layer.open(jsmile.dialog.init({
                title: "新增部门",
                content: "sys/dept/add.html",
                success: function(layero,index){
                    var iframeW=layero.find('iframe')[0];
                    iframeW.contentWindow.app.dept=node?{parentId:node.id}:{};
                }
            }));
        },
        editDept: function(){
            var _this=this;
            var node=this.$refs.deptTree.getCurrentNode();
            if(!node){
                top.app.$alert('请选择需要修改的部门!', '提示',{type: 'warning'});
                return false;
            }
            top.layer.open(jsmile.dialog.init({
                title: "修改部门信息",
                content: "sys/dept/edit.html",
                success: function(layero,index){
                    var iframeW=layero.find('iframe')[0];
                    iframeW.contentWindow.app.dept=node;
                }
            }));
        },
        deleteDept: function(){
            var _this=this;
            var node=this.$refs.deptTree.getCurrentNode();
            if(!node){
                top.app.$alert('请选择需要修改的部门!', '提示',{type: 'warning'});
                return false;
            }
            top.app.$confirm('此操作将永久删除该部门及子部门, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function(){
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/dept/delete",
                    data:{"id":node.id}
                }).done(function(res){
                    jsmile.msg.success("删除部门成功");
                    _this.getDeptTree();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"删除部门失败!");
                });
            }).catch(function(){
                jsmile.msg.info( '已取消删除');
            });
        },
        getDeptTree: function(){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/dept/getList"
            }).done(function(res){
                var data=jsmile.tree.toTree(res.resData,{"textField":"deptName"});
                _this.deptTree=data;
            });
        },
        goHome: function(url){
            top.$('#home').attr("src","homePage/home.html");
        }
    }
});
$(function(){
    init();
    function init(){
        app.init();
    }
});